package com.rory.tiny.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rory.tiny.common.enums.RedisKeyPrefixEnum;
import com.rory.tiny.common.service.RedisService;
import com.rory.tiny.modules.pms.dto.request.ProvinceSelectRequestDTO;
import com.rory.tiny.modules.pms.dto.response.ProvinceSelectResponseDTO;
import com.rory.tiny.modules.pms.mapper.PmsProvinceSelectMapper;
import com.rory.tiny.modules.pms.model.PmsProvinceSelect;
import com.rory.tiny.modules.pms.service.PmsProvinceSelectService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rory
 * @since 2022-12-27
 */
@Service
public class PmsProvinceSelectServiceImpl extends ServiceImpl<PmsProvinceSelectMapper, PmsProvinceSelect> implements PmsProvinceSelectService {

	@Resource
	private PmsProvinceSelectMapper mapper;

	@Resource
	private RedisService redisService;

	/**
	 * 新增省份选择次数数据
	 *
	 * @param pmsProvinceSelect
	 * @return
	 * @author zhangzedi
	 * @date 2022/12/27 23:03
	 */
	@Override
	public int insert(PmsProvinceSelect pmsProvinceSelect) {
		int res;
		QueryWrapper<PmsProvinceSelect> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("area_code", pmsProvinceSelect.getAreaCode());
		List<PmsProvinceSelect> resultList = mapper.selectList(queryWrapper);
		if (CollectionUtils.isEmpty(resultList)) {
			res = mapper.insert(pmsProvinceSelect);
		} else {
			UpdateWrapper<PmsProvinceSelect> updateWrapper = new UpdateWrapper<>();
			updateWrapper.set("select_num", pmsProvinceSelect.getSelectNum());
			updateWrapper.eq("area_code", pmsProvinceSelect.getAreaCode());
			res = mapper.update(pmsProvinceSelect, updateWrapper);
		}
		return res;
	}

	/**
	 * 省份选择
	 *
	 * @param dto
	 * @return {@link int}
	 * @author zhangzedi
	 * @date 2022/12/27 23:09
	 */
	@Override
	public long select(ProvinceSelectRequestDTO dto) {
		long res;
		Object numObj = redisService.get(RedisKeyPrefixEnum.AREA_SELECT_NUM.getPrefix() + dto.getAreaCode());
		if (Objects.isNull(numObj)) {
			res = 1L;
			redisService.set(RedisKeyPrefixEnum.AREA_SELECT_NUM.getPrefix() + dto.getAreaCode(), res);
		} else {
			res = redisService.incr(RedisKeyPrefixEnum.AREA_SELECT_NUM.getPrefix() + dto.getAreaCode(), 1);
		}
		return res;
	}

	/**
	 * 查询热门身份
	 *
	 * @return
	 * @author zhangzedi
	 * @date 2022/12/27 23:13
	 */
	@Override
	public List<ProvinceSelectResponseDTO> queryHotArea() {
		//返回结果
		List<ProvinceSelectResponseDTO> responseDTOList;
		Object obj = redisService.get(RedisKeyPrefixEnum.HOT_AREA_LIST.getPrefix());
		if (obj != null) {
			responseDTOList = (List<ProvinceSelectResponseDTO>) obj;
		} else {
			QueryWrapper<PmsProvinceSelect> queryWrapper = new QueryWrapper<>();
			queryWrapper.orderByDesc("select_num");
			List<PmsProvinceSelect> pmsProvinceSelectList = mapper.selectList(queryWrapper);
			responseDTOList = new ArrayList<>(pmsProvinceSelectList.size());
			pmsProvinceSelectList.forEach(item -> {
				ProvinceSelectResponseDTO dto = new ProvinceSelectResponseDTO();
				dto.setAreaCode(item.getAreaCode());
				dto.setSelectNum(item.getSelectNum());
				responseDTOList.add(dto);
			});
			redisService.set(RedisKeyPrefixEnum.HOT_AREA_LIST.getPrefix(), responseDTOList, 3 * 60L);
		}
		return responseDTOList;
	}
}
